
#include "FxEclipse.h"
#include "ResourceManager.h"

FxEclipse::FxEclipse(Vector3 _origin)
	:ParticleManager(_origin)
{
	isDeath = false;
	m_fLife = 3.0f;
	m_size = Vector3(1, 0, 1);
	m_color = Color4(1.0f, 1.0f, 0.0f, 1.0f);
	m_position = Vector3(_origin.X, _origin.Y, _origin.Z);
	m_accelerator = Vector3(0, 0, 0);
	m_velocity = Vector3(0, 0, 0);
	m_listParticle = vector<Particle*>();
	m_angle = 0;
	Generate(1);
}


FxEclipse::~FxEclipse(void)
{
}

void FxEclipse::InitParticle()
{
	Vector3 _acc = Vector3(0, 0.0003f, 0);
	Vector3 _position = Vector3(0, 0, 0);
	m_listParticle.push_back(new Particle(m_fLife, m_size, _position, _acc, m_color));

}
void FxEclipse::Update(float _dt)
{
	if(m_listParticle.size() == 0) 
	{
		isDeath = true;
		return;
	}
	for(int i = 0; i < m_listParticle.size(); i++)
	{
		Particle *p;
		p = m_listParticle[i];

		if(p->m_color.A < 0)
		{
			m_listParticle.erase(m_listParticle.begin() + i);
			//InitParticle();
		}
		else
		{
			
			p->m_position.Y += p->m_accelerator.Y * _dt;

			p->m_color.A -= 0.0005  * _dt ;
			//p->m_color.R -= 0.005 * _dt;
			//p->m_color.B += 0.005 * _dt;
			//p->m_size.X -= 0.0001;
			//p->m_size.Z -= 0.0001;
		}


	}
	
	//m_fLife -= 0.001;
}

void FxEclipse::Draw(float _posX, float _posY, float _posZ, float _viewX, float _viewY, float _viewZ)
{
	glPushMatrix();

	ResourceManager::GetInst()->LoadTexture("Resource/texture/eclipse.png");
	glDepthMask(false);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	glEnable(GL_TEXTURE_2D);

	glTranslatef(m_position.X, m_position.Y, m_position.Z);

	for(int i = 0; i < m_listParticle.size(); i++)
	{
		Particle *p;
		p = m_listParticle[i];

		glColor4f(p->m_color.R, p->m_color.G, p->m_color.B, p->m_color.A);

		glPushMatrix();

		glTranslatef(p->m_position.X, p->m_position.Y, p->m_position.Z);
		glRotatef(m_angle, 0, 1, 0);
		m_angle += 5;
		float _sizeX = p->m_size.X / 2;
		float _sizeY = p->m_size.Y / 2;
		float _sizeZ = p->m_size.Z / 2;
		glBegin(GL_QUADS);
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-_sizeX, -_sizeY,  _sizeZ);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( _sizeX, -_sizeY,  _sizeZ);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( _sizeX,  _sizeY, -_sizeZ);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-_sizeX,  _sizeY, -_sizeZ);
		glEnd();

		glPopMatrix();

		//glDisable(GL_BLEND);
		////glBlendFunc(GL_SRC_ALPHA, GL_ONE);
		//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		//glDepthMask(true);
	}
	glDisable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	glDisable(GL_TEXTURE_2D);
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDepthMask(true);
	glPopMatrix();
}
